package com.rhino.security.service;

import com.rhino.security.vo.JwtUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Collection;

/**
 * @ClassName AuthorityPermissionService
 * @Description 自定义业务逻辑，判断是否允许访问某个Action
 * @Author zyk
 * @Create 2021/7/12
 * @Version 1.0
 **/
@Component("authorityPermission")
public class AuthorityPermissionService {
    public boolean hasPermission(String permissions) {
        if (StringUtils.isEmpty(permissions)) {
            return false;
        }
        // 用户信息对象
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        Object credentials = authentication.getCredentials();
        Object principal = authentication.getPrincipal();

        //获取当前用户信息
        JwtUser user=(JwtUser) authentication.getPrincipal();
        Collection<GrantedAuthority> authorities1 = user.getAuthorities();
        //业务上可能判断当前用户所属组织、部门等判断是否可以访问
        if(permissions.equalsIgnoreCase("Admin")
        &&user.getUsername().equalsIgnoreCase("jake_j")){
            return true;
        }
        return false;
    }
}
